package Concurrent.Lock;

import java.util.Collections;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wangyg6 on 2017/11/30.
 */
public class DeadLockDemo {

	public static void main(String[] args) {
		ReentrantLock lock = new ReentrantLock();
		Condition a = lock.newCondition();
		Condition b = lock.newCondition();



		new Thread("A") {
			@Override
			public void run() {
				lock.lock();
				try {
					for (int i = 1; i <= 10; i++) {
						a.await();
						System.out.print(Thread.currentThread().getName());
						b.signal();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}
		}.start();
		new Thread("B") {
			@Override
			public void run() {
				lock.lock();
				try {
					for (int i = 1; i <= 10; i++) {
						b.await();
						System.out.print(Thread.currentThread().getName());
						a.signal();
					}

				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}.start();

	}
}
